import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Utils {
  static void toast(String value) async {
    print(value);
    Fluttertoast.showToast(
        msg: value,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 3,
        backgroundColor: Colors.black54,
        textColor: Colors.white,
        fontSize: 17);
  }

  static Future<String> getStrSharePreferences(String key) async {
    final prefs = await SharedPreferences.getInstance();
    if (prefs != null) {
      return prefs.getString(key);
    }
    return null;
  }

  static putStrSharePreferences(String key, String value) async {
    final prefs = await SharedPreferences.getInstance();
    Future<bool> save = prefs.setString(key, value);
    save.then((value) => print);
  }

  static putIntSharePreferences(String key, int value) async {
    final prefs = await SharedPreferences.getInstance();
    Future<bool> save = prefs.setInt(key, value);
    save.then((value) => print);
  }

  static Future<int> getIntSharePreferences(String key) async {
    final prefs = await SharedPreferences.getInstance();
    if (prefs != null) {
      if (prefs.getInt(key) != null) {
        return prefs.getInt(key);
      } else {
        return 1;
      }
    }
    return -1;
  }

  static putBoolSharePreferences(String key, bool value) async {
    final prefs = await SharedPreferences.getInstance();
    Future<bool> save = prefs.setBool(key, value);
    save.then((value) => print);
  }

  static Future<bool> getBoolSharePreferences(String key) async {
    final prefs = await SharedPreferences.getInstance();
    if (prefs != null) {
      if (prefs.getBool(key) != null) {
        return prefs.getBool(key);
      } else {
        return false;
      }
    }
    return false;
  }

  static bool phoneStr(String str) {
    RegExp exp = RegExp(
        r'^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$');
    bool matched = exp.hasMatch(str);
    // if(!matched){
    //   toast("输入的手机号码不规范");
    // }
    return matched;
  }
}
